package com.hoo.service.account;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.mapper.BeanMapper;

import com.hoo.common.constant.Constant;
import com.hoo.common.constant.Result;
import com.hoo.entity.account.Users;
import com.hoo.exception.SaleException;
import com.hoo.form.account.UsersForm;
import com.hoo.mapper.account.AccountMapper;
import com.hoo.util.AuthUtils;
import com.hoo.util.DateUtils;
import com.hoo.util.PaginationSupport;

@SuppressWarnings("unchecked")
@Service
public class AccountService<T extends Users> {
	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	public static final int SALT_SIZE = 8;
	private static Logger logger = LoggerFactory.getLogger(AccountService.class);

	@Inject
	private AccountMapper accountMapper;

	public boolean addUser(Users user) throws DataAccessException {
		boolean flag = false;
		try {
			//String[] saltAndPassword = AuthUtils.generateSaltAndPassword("123456");
			accountMapper.insertUser(user);
			flag = true;
		} catch (DataAccessException e) {
			flag = false;
			throw e;
		}
		return flag;
	}
	/* public Result saveUser(UsersForm form) {
	        Result result = new Result();
	        Map<String, Object> param = new HashMap<String, Object>();
	        param.put("loginName", form.getLoginName());
	        String[] saltAndPassword = AuthUtils.generateSaltAndPassword("123456");
	        form.setLoginName(form.getLoginName());
	        form.setUserCode(form.getLoginName());
	        form.setSalt(saltAndPassword[0]);
	        form.setLoginPassword(saltAndPassword[1]);
	        form.setLoginCount(0);
	        form.setUserFlag(1);
	        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(Constant.DATE_FORMAT_SIMPLE);
	        try {
				 form.setAddTime(simpleDateFormat.parse(DateUtils.getCurrentDate()));
				 form.setUpdateTime(simpleDateFormat.parse(DateUtils.getCurrentDate()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
	        Users users=BeanMapper.map(form, Users.class);
	        accountMapper.insertUser(users);
	        return result;
	    }*/
	
	public static void main(String[] args) {
		 String[] saltAndPassword = AuthUtils.generateSaltAndPassword("123");
		 System.err.println(saltAndPassword[0]);
		 System.err.println(saltAndPassword[1]);
	}
	 public int saveUser(UsersForm form) throws SaleException {
	        Map<String, Object> param = new HashMap<String, Object>();
	        param.put("loginName", form.getLoginName());
	        String[] saltAndPassword = AuthUtils.generateSaltAndPassword("123456");
	        form.setLoginName(form.getLoginName());
	        form.setUserCode(form.getLoginName());
	        form.setSalt(saltAndPassword[0]);
	        form.setLoginPassword(saltAndPassword[1]);
	        form.setLoginCount(0);
	        form.setUserFlag(1);
	        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(Constant.DATE_FORMAT_SIMPLE);
	        try {
				 form.setAddTime(simpleDateFormat.parse(DateUtils.getCurrentDate()));
				 form.setUpdateTime(simpleDateFormat.parse(DateUtils.getCurrentDate()));
			} catch (ParseException e) {
				throw new SaleException(e.getMessage());
			}
	        Users users=BeanMapper.map(form, Users.class);
	        return accountMapper.insertUser(users);
	    }

	public Users getAccountById(Integer id) throws DataAccessException {
		return accountMapper.getUserById(id);
	}

	public List<Users> getUserList(UsersForm form) throws DataAccessException {
		return accountMapper.getUserlist(form);
	}
	
	public PaginationSupport<Users> searchUserList(UsersForm form) {
        int totalCount = this.accountMapper.getUsersCount(form);
        List<Users> list =this.getUserList(form);
        return new PaginationSupport(list, totalCount, form.getiDisplayLength(), form.getiDisplayStart());
    }
	
	public Users getUserByLoginName(String loginName){
		return this.accountMapper.getUserByLoginName(loginName);
	}
	
	public Users getUserByRealName(String realName) {
		return accountMapper.getUserByRealName(realName);
	}

	public int updateUserLoginInfo(Users user) {
		return accountMapper.updateUser(user);
	}
	
   @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public int updateUser(UsersForm form) throws SaleException {
        Users user = BeanMapper.map(form, Users.class);
        user.setUpdateTime(new Date());
        return accountMapper.updateUser(user);
    }
}